/**
 * 
 */
package info.niwota.frames;

import info.niwota.frames.Dialogs.ItemInfo;
import info.niwota.frames.Dialogs.ItemListAdapter;

import java.io.File;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;

/**
 * @author qiangli
 *
 */
public class FrameMultiSelectImageActivity extends MultiSelectImageActivity {
	private static final boolean DEBUG = Debug.DEBUG;
	private static final String TAG = "FrameMultiSelectImageActivity";
	
	private static final int DIALOG_LISTFRAME = 1;
	
	private ItemListAdapter localAdapter;
	
	private ImageFrame frame;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    showDialog(DIALOG_LISTFRAME);
	}

	@Override
	protected boolean isScanning() {
		return false;
	}

	@Override
	protected void registerReceivers() {
		//do nothing
	}
	
	@Override
	protected void unregisterReceiver() {
		//do nothing
	}

	@Override
	protected void showNoImage() {
		if (frame == null) {
			mNoImagesView.setVisibility(View.GONE);
		} else {
			super.showNoImage();
		}
	}

	@Override
	protected IImageList createImageList() {
		return frame == null ? null : frame.getList();
	}
	
	private Dialog createFramesDialog() {
		final Context ctx = this;
		
		localAdapter = new ItemListAdapter(this) {

			public void loadItems(final Context context, List<ItemInfo> list) {
				if (DEBUG) {
					Log.d(TAG, "load : " + list);
				}
				
				File[] files = FrameHelper.list(ctx);
				for (int i = 0; i < files.length; i++) {
					File f = files[i];
					ItemInfo item = new ItemInfo();
					item.setTitle(f.getName());
					item.setIconRes(0);
					item.setIcon(null);
					item.setObject(f);
					//
					list.add(item);
				}
			}

			public void onClick(DialogInterface dialog, int which) {
				if (DEBUG) {
					Log.d(TAG, "onClick which:  " + which);
				}
				ItemInfo item = getItem(which);
				File file = null;
				if (item != null) {
					file = (File) item.getObject();
				}
				if (DEBUG) {
					Log.d(TAG, "onClick file: " + file);
				}
				
				loadFrame(file, file.lastModified());
			}
		};
		CharSequence title = ctx.getText(R.string.my_frames);
		AlertDialog d = Dialogs.create(ctx, title, localAdapter);
		d.setOnCancelListener(new OnCancelListener() {
			public void onCancel(DialogInterface arg0) {
				finish();
			}
			
		});
		return d;
		
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		if (id == DIALOG_LISTFRAME) {
			final Window w = dialog.getWindow();
			Issues.fixIssue3674(w);
		}
		switch (id) {
		case DIALOG_LISTFRAME:
			if (localAdapter != null) {
				localAdapter.reload();
			}
			break;
		default:
		}
	}

	protected void loadFrame(File file, long lastModified) {
		try {
			frame = new ImageFrame();
			frame.setStore(file);
			frame.setLastModified(lastModified);
			frame.restore(this);
			
			rebake(false, false);
		} catch (Exception e) {
			showError(e);
		}
	}
	
	@Override
	protected boolean isMultiSelect() {
		return true;
	}

	@Override
	protected void rebake(boolean unmounted, boolean scanning) {
		if (frame != null) {
			super.rebake(unmounted, scanning);
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_LISTFRAME:
			return createFramesDialog();
		default:
		}
		return super.onCreateDialog(id);
	}
}
